#################################################################################### 
####################################################################################
####################################################################################
# Appendix G: Tables G.20 - G.23, M.53-M.56
####################################################################################
####################################################################################
library(here)
library(kableExtra)
library(lmtest)
library(multiwayvcov)
####################################################################################
####################################################################################
## Read in survey data (from: 8_2020_analysis.R)
dat = readRDS(here("data/dat_unrestricted.rds"))
datb = readRDS(here("data/dat_restricted.rds"))

# Implement SWE of ATE: Demean all covariates, fully interact
cols = c("sex","age","education_level","Arua","Bushenyi","Ibanda","Jinja","Mbale","Mpigi","Pallisa","Mbarara","Shema",
         "village_distance_HF","village_distance_HOSP","village_distance_road","village_distance_electricity", "share2006")
dat[, paste0("c_", cols)] = lapply(dat[, cols], function(x) (x - mean(x))  )
covariates = c("treatment*c_village_distance_HF + treatment*c_village_distance_HOSP + treatment*c_village_distance_road + treatment*c_village_distance_electricity + 
                treatment*c_sex + c_age*treatment + c_education_level*treatment + treatment*c_share2006 + 
                c_Arua*treatment + c_Bushenyi*treatment + c_Ibanda*treatment + c_Jinja*treatment + c_Mbale*treatment + c_Mpigi*treatment + c_Pallisa*treatment + c_Mbarara*treatment",
               "c_Arua*treatment + c_Bushenyi*treatment + c_Ibanda*treatment + c_Jinja*treatment + c_Mbale*treatment + c_Mpigi*treatment + c_Pallisa*treatment + c_Mbarara*treatment")

# Implement SWE of ATE: Demean all covariates, fully interact
cols = c("sex","age","education_level","Arua","Ibanda","Pallisa","Shema",
         "village_distance_HF","village_distance_HOSP","village_distance_road","village_distance_electricity", "share2006")
datb[, paste0("c_", cols)] = lapply(datb[, cols], function(x) (x - mean(x))  )
covariatesb = c("treatment*c_village_distance_HF + treatment*c_village_distance_HOSP + treatment*c_village_distance_road + treatment*c_village_distance_electricity + 
                treatment*c_sex + c_age*treatment + c_education_level*treatment + treatment*c_share2006 + 
                c_Arua*treatment + c_Ibanda*treatment + c_Pallisa*treatment", 
                "c_Arua*treatment + c_Ibanda*treatment + c_Pallisa*treatment")

####################################################################################
####################################################################################
## Trust ####

####################################################################################
## Spending

# Unrestricted
for (i in names(dat[c("spent_ng","wasted_ng","stolen_ng",
                      "spent_dg","wasted_dg","stolen_dg",
                      "spent_ngos","wasted_ngos","stolen_ngos")])){ # Dependent Variables
  for (j in covariates) {
    model = paste(i,"~","treatment","+", j)
    
    # Run each model
    assign(x = paste("m",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = dat))
    # Output clustered SEs (county)
    assign(x = paste("c",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = dat),
                            cluster.vcov(lm(as.formula(model), data = dat), dat$village_id)))
  }
}

# Restricted
for (i in names(datb[c("spent_ng","wasted_ng","stolen_ng",
                       "spent_dg","wasted_dg","stolen_dg",
                       "spent_ngos","wasted_ngos","stolen_ngos")])){ # Dependent Variables
  for (j in covariatesb) {
    model = paste(i,"~","treatment","+", j)
    
    # Run each model
    assign(x = paste("bm",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = datb))
    # Output clustered SEs (county)
    assign(x = paste("bc",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = datb),
                            cluster.vcov(lm(as.formula(model), data = datb), datb$village_id)))
  }
}

####################################################################################
####################################################################################
## Table G.20

stargazer(m.spent_dg.t, m.wasted_dg.t, m.stolen_dg.t, 
          m.spent_ng.t, m.wasted_ng.t, m.stolen_ng.t,
          m.spent_ngos.t, m.wasted_ngos.t, m.stolen_ngos.t,
          
          se = list(c.spent_dg.t[,2], c.wasted_dg.t[,2], c.stolen_dg.t[,2], 
                    c.spent_ng.t[,2], c.wasted_ng.t[,2], c.stolen_ng.t[,2],
                    c.spent_ngos.t[,2], c.wasted_ngos.t[,2], c.stolen_ngos.t[,2]),
          
          
          p = list(c.spent_dg.t[,4], c.wasted_dg.t[,4], c.stolen_dg.t[,4], 
                   c.spent_ng.t[,4], c.wasted_ng.t[,4], c.stolen_ng.t[,4],
                   c.spent_ngos.t[,4], c.wasted_ngos.t[,4], c.stolen_ngos.t[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/trust_1_urc.tex",
          label = "tab:trust_1_urc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Spending",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Spent","Wasted", "Stolen","Spent","Wasted", "Stolen","Spent","Wasted", "Stolen"))
####################################################################################
####################################################################################
## Table M.53

stargazer(m.spent_dg.c, m.wasted_dg.c, m.stolen_dg.c, 
          m.spent_ng.c, m.wasted_ng.c, m.stolen_ng.c,
          m.spent_ngos.c, m.wasted_ngos.c, m.stolen_ngos.c,
          
          se = list(c.spent_dg.c[,2], c.wasted_dg.c[,2], c.stolen_dg.c[,2], 
                    c.spent_ng.c[,2], c.wasted_ng.c[,2], c.stolen_ng.c[,2],
                    c.spent_ngos.c[,2], c.wasted_ngos.c[,2], c.stolen_ngos.c[,2]),
          
          
          p = list(c.spent_dg.c[,4], c.wasted_dg.c[,4], c.stolen_dg.c[,4], 
                   c.spent_ng.c[,4], c.wasted_ng.c[,4], c.stolen_ng.c[,4],
                   c.spent_ngos.c[,4], c.wasted_ngos.c[,4], c.stolen_ngos.c[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/trust_1_urnc.tex",
          label = "tab:trust_1_urnc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Spending (No covariates)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Spent","Wasted", "Stolen","Spent","Wasted", "Stolen","Spent","Wasted", "Stolen"))


####################################################################################
####################################################################################
## Table G.21

stargazer(bm.spent_dg.t, bm.wasted_dg.t, bm.stolen_dg.t, 
          bm.spent_ng.t, bm.wasted_ng.t, bm.stolen_ng.t,
          bm.spent_ngos.t, bm.wasted_ngos.t, bm.stolen_ngos.t,
          
          se = list(bc.spent_dg.t[,2], bc.wasted_dg.t[,2], bc.stolen_dg.t[,2], 
                    bc.spent_ng.t[,2], bc.wasted_ng.t[,2], bc.stolen_ng.t[,2],
                    bc.spent_ngos.t[,2], bc.wasted_ngos.t[,2], bc.stolen_ngos.t[,2]),
          
          
          p = list(bc.spent_dg.t[,4], bc.wasted_dg.t[,4], bc.stolen_dg.t[,4], 
                   bc.spent_ng.t[,4], bc.wasted_ng.t[,4], bc.stolen_ng.t[,4],
                   bc.spent_ngos.t[,4], bc.wasted_ngos.t[,4], bc.stolen_ngos.t[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/trust_1_rc.tex",
          label = "tab:trust_1_rc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Spending (Restircted)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Spent","Wasted", "Stolen","Spent","Wasted", "Stolen","Spent","Wasted", "Stolen"))

####################################################################################
####################################################################################
## Table M.54

stargazer(bm.spent_dg.c, bm.wasted_dg.c, bm.stolen_dg.c, 
          bm.spent_ng.c, bm.wasted_ng.c, bm.stolen_ng.c,
          bm.spent_ngos.c, bm.wasted_ngos.c, bm.stolen_ngos.c,
          
          se = list(bc.spent_dg.c[,2], bc.wasted_dg.c[,2], bc.stolen_dg.c[,2], 
                    bc.spent_ng.c[,2], bc.wasted_ng.c[,2], bc.stolen_ng.c[,2],
                    bc.spent_ngos.c[,2], bc.wasted_ngos.c[,2], bc.stolen_ngos.c[,2]),
          
          
          p = list(bc.spent_dg.c[,4], bc.wasted_dg.c[,4], bc.stolen_dg.c[,4], 
                   bc.spent_ng.c[,4], bc.wasted_ng.c[,4], bc.stolen_ng.c[,4],
                   bc.spent_ngos.c[,4], bc.wasted_ngos.c[,4], bc.stolen_ngos.c[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/trust_1_rnc.tex",
          label = "tab:trust_1_rnc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Spending (Restircted; No covariates)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Spent","Wasted", "Stolen","Spent","Wasted", "Stolen","Spent","Wasted", "Stolen"))

# Post-processing
# & \multicolumn{3}{c}{District Govt} & \multicolumn{3}{c}{National Govt} & \multicolumn{3}{c}{NGOs} \\ 
# \cmidrule(lr){2-4} \cmidrule(l){5-7} \cmidrule(l){8-9} 
rm(list =  grep("^(c|m)\\.", ls(), value = T))
rm(list = grep("^(bc|bm)\\.", ls(), value = T))

####################################################################################
## Trust

# Unrestricted
for (i in names(dat[c("trust_people_gov_index","trust_people_mp_st","trust_people_lc_st","trust_people_ga_st",
                      "trust_people_pres_st","trust_people_ngo_st")])){ # Dependent Variables
  for (j in covariates) {
    model = paste(i,"~","treatment","+", j)
    
    # Run each model
    assign(x = paste("m",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = dat))
    # Output clustered SEs (county)
    assign(x = paste("c",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = dat),
                            cluster.vcov(lm(as.formula(model), data = dat), dat$village_id)))
  }
}

# Restricted
for (i in names(datb[c("trust_people_gov_index","trust_people_mp_st","trust_people_lc_st","trust_people_ga_st",
                       "trust_people_pres_st","trust_people_ngo_st")])){ # Dependent Variables
  for (j in covariatesb) {
    model = paste(i,"~","treatment","+", j)
    
    # Run each model
    assign(x = paste("bm",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = datb))
    # Output clustered SEs (county)
    assign(x = paste("bc",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = datb),
                            cluster.vcov(lm(as.formula(model), data = datb), datb$village_id)))
  }
}

####################################################################################
####################################################################################
## Table G.22

stargazer(m.trust_people_gov_index.t, m.trust_people_lc_st.t, m.trust_people_mp_st.t, m.trust_people_pres_st.t, 
          m.trust_people_ga_st.t, m.trust_people_ngo_st.t,
          
          se = list(c.trust_people_gov_index.t[,2], c.trust_people_lc_st.t[,2], c.trust_people_mp_st.t[,2], c.trust_people_pres_st.t[,2], 
                    c.trust_people_ga_st.t[,2], c.trust_people_ngo_st.t[,2]),
          
          p = list(c.trust_people_gov_index.t[,4], c.trust_people_lc_st.t[,4], c.trust_people_mp_st.t[,4], c.trust_people_pres_st.t[,4], 
                   c.trust_people_ga_st.t[,4], c.trust_people_ngo_st.t[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/trust_2_urc.tex",
          label = "tab:trust_2_urc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Trust",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Govt", "Local", "MP", "President","Government",
                            "NGO\\\\ & Index & Councilors & & & Agency & "))

####################################################################################
####################################################################################
## Table M.55

stargazer(m.trust_people_gov_index.c, m.trust_people_lc_st.c, m.trust_people_mp_st.c, m.trust_people_pres_st.c, 
          m.trust_people_ga_st.c, m.trust_people_ngo_st.c,
          
          se = list(c.trust_people_gov_index.c[,2], c.trust_people_lc_st.c[,2], c.trust_people_mp_st.c[,2], c.trust_people_pres_st.c[,2], 
                    c.trust_people_ga_st.c[,2], c.trust_people_ngo_st.c[,2]),
          
          p = list(c.trust_people_gov_index.c[,4], c.trust_people_lc_st.c[,4], c.trust_people_mp_st.c[,4], c.trust_people_pres_st.c[,4], 
                   c.trust_people_ga_st.c[,4], c.trust_people_ngo_st.c[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/trust_2_urnc.tex",
          label = "tab:trust_2_urnc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Trust (No covariates)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Govt", "Local", "MP", "President","Government",
                            "NGO\\\\ & Index & Councilors & & & Agency & "))


####################################################################################
####################################################################################
## Table G.23

stargazer(bm.trust_people_gov_index.t, bm.trust_people_lc_st.t, bm.trust_people_mp_st.t, bm.trust_people_pres_st.t, 
          bm.trust_people_ga_st.t, bm.trust_people_ngo_st.t,
          
          se = list(bc.trust_people_gov_index.t[,2], bc.trust_people_lc_st.t[,2], bc.trust_people_mp_st.t[,2], bc.trust_people_pres_st.t[,2], 
                    bc.trust_people_ga_st.t[,2], bc.trust_people_ngo_st.t[,2]),
          
          
          p = list(bc.trust_people_gov_index.t[,4], bc.trust_people_lc_st.t[,4], bc.trust_people_mp_st.t[,4], bc.trust_people_pres_st.t[,4], 
                   bc.trust_people_ga_st.t[,4], bc.trust_people_ngo_st.t[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/trust_2_rc.tex",
          label = "tab:trust_2_rc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Trust (Restricted)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Govt", "Local", "MP", "President","Government",
                            "NGO\\\\ & Index & Councilors & & & Agency & "))

####################################################################################
####################################################################################
## Table M.56

stargazer(bm.trust_people_gov_index.c, bm.trust_people_lc_st.c, bm.trust_people_mp_st.c, bm.trust_people_pres_st.c, 
          bm.trust_people_ga_st.c, bm.trust_people_ngo_st.c,
          
          se = list(bc.trust_people_gov_index.c[,2], bc.trust_people_lc_st.c[,2], bc.trust_people_mp_st.c[,2], bc.trust_people_pres_st.c[,2], 
                    bc.trust_people_ga_st.c[,2], bc.trust_people_ngo_st.c[,2]),
          
          
          p = list(bc.trust_people_gov_index.c[,4], bc.trust_people_lc_st.c[,4], bc.trust_people_mp_st.c[,4], bc.trust_people_pres_st.c[,4], 
                   bc.trust_people_ga_st.c[,4], bc.trust_people_ngo_st.c[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/trust_2_rnc.tex",
          label = "tab:trust_2", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Trust (Restricted; No covariates)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Govt", "Local", "MP", "President","Government",
                            "NGO\\\\ & Index & Councilors & & & Agency & "))

rm(list =  grep("^(c|m)\\.", ls(), value = T))
rm(list = grep("^(bc|bm)\\.", ls(), value = T))